/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gblend.largegraph;

import gblend.adjlistgraph.ALGraph;
import gblend.adjlistgraph.FsgIdEntry;
import gblend.frequentindex.Vertex;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Vector;
import org.openide.util.Exceptions;

/**
 *
 * @author HO HOANG HUNG
 */
public class Conversion {
    //Index is id,value is label

    private static String[] labelmap = null;
    private static String labelf = null;

    public static void setLabelFile(String path) {
        labelf = new String(path);
    }

    /**
     * Single graph file in metis format
     * @param path use '/'
     * @return
     */
    public static ALGraph combinedToAL(String path) {
        //GRAPH NAY PHAI CO DAY DU THONG TIN VE Boundary vertices
        //CAN PHAI SET FsgIdEntry attribute cho graph nay, dua vao ten
        //RETURN NULL if there is no file exit in this path
        if (labelmap == null) {
            readLabel();
        }
        System.out.println("combinedToAL path="+path);
        int start = path.lastIndexOf("/");
        String[] idstr = path.substring(start + 1).trim().split("-");
        String newpath = path.substring(0, start)+"extra/"+path.substring(start+1);
        File fl = new File(newpath);
        if(!fl.exists()){
            FsgIdEntry empty = new FsgIdEntry();
            FsgIdEntry whole = new FsgIdEntry(Integer.parseInt(idstr[0]));
            for(int i=1;i<idstr.length;i++){
                whole.addGraphId(Integer.parseInt(idstr[i]));
            }
            CombineGraph.combineGraphs(path.substring(0, start+1), whole, empty);
        }

        BufferedInputStream bin1;
        String[] split;
        Vector<Integer>[] adjlist;
        HashMap<Integer, Integer> nodeidmap;
        try {
            bin1 = new BufferedInputStream(new FileInputStream(fl));
            BufferedReader br1 = new BufferedReader(new InputStreamReader(bin1));
            String line = br1.readLine();
            int nnum = Integer.parseInt(line.trim().split("\\s")[1]);
            adjlist = new Vector[nnum];
            for (int j = 0; j < adjlist.length; j++) {
                adjlist[j] = new Vector<Integer>();
            }
            nodeidmap = new HashMap<Integer, Integer>(nnum);
            for (int i = 0; i < nnum; i++) {
                line = br1.readLine();
                split = line.trim().split("\\s");
                int length = split.length;
                for (int k = 0; k < length; k++) {
                    adjlist[i].add(Integer.parseInt(split[k]));
                }
            }
            bin1.close();
            br1.close();
            ALGraph result = new ALGraph(nnum);
            for (int i = 0; i < idstr.length; i++) {
                result.setGraphid(Integer.parseInt(idstr[i]));
            }
            result.setNnum(nnum);
            for (int j = 0; j < adjlist.length; j++) {
                int origin = adjlist[j].get(0);
                nodeidmap.put(origin, j);
            }
            int edgenum = 0;
            for (int i = 0; i < nnum; i++) {
                Vertex node = new Vertex();
                //  String[] nodeline = strLine.split("\\s");
                int globalid = adjlist[i].firstElement();
                node.setLabel(labelmap[globalid-1]);
                node.setId(i);
                node.setRealDegree(adjlist[i].lastElement());
                result.addnode(node);
            }

            for (int i = 0; i < nnum; i++) {
               // String[] info = col.get(i);
               // int from = Integer.parseInt(info[0]);
                int length = adjlist[i].size() - 1;
                for (int j = 1; j < length; j++) {

                     Integer to1 = nodeidmap.get(adjlist[i].elementAt(j));
                     if(nodeidmap ==null){
                         System.out.println("nodeidmap null");
                     }else if(adjlist[i] ==null){
                         System.out.println("adjlist["+i+"] null");
                     }else if(to1 ==null){
                         System.out.println("to1 null "+i+" j="+j+" adjlist[i].elementAt(j)="+adjlist[i].elementAt(j)+"*");
                     }
                     int to = to1;
                    if (i < to) {
                      //  System.out.println("from=" + i + ",to=" + to);
                        edgenum++;
                        
                        result.addEdge(i, to);
                        result.getNode(i).setDegree();
                        result.getNode(to).setDegree();
                        result.getNode(i).setIn(to);
                        result.getNode(to).setIn(i);
                    }
                }
            }
           /* Vertex[] vts = result.getVertices();
            for (int i = 0; i < vts.length; i++) {
                if (vts[i].getDegree() < vts[i].getRealDegree()) {
                    result.addBoundaryVertex(vts[i].getId());
                }
            }*/
            result.setEnum(edgenum);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    private static void readLabel() {
        BufferedInputStream bin = null;
        try {
            File resFile = new File(labelf);
            bin = new BufferedInputStream(new FileInputStream(resFile));
            BufferedReader br = new BufferedReader(new InputStreamReader(bin));
            String strLine = br.readLine();
            labelmap = new String[Integer.parseInt(strLine)];
            for (int i = 0; i < labelmap.length; i++) {
                strLine = br.readLine();
                labelmap[i] = strLine;
            }
            bin.close();
            br.close();
        } catch (Exception ex) {
            Exceptions.printStackTrace(ex);
        } 
    }
}
